using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using NBusiness.Properties;
using NBusiness.CodeDom;

namespace NBusiness.Data.Validation
{
    [Serializable]
    [ExpectedArgumentType(typeof(RegexEventArgs))]
    public sealed class RegexRule : ValidationRule
    {
        public RegexRule(Type entityType, string field, RegexEventArgs e)
            : base(entityType, field, ValidateRegexRule, e, Resources.RegexRuleMessage)
        { }

        public static bool ValidateRegexRule(object value, ValidationEventArgs e)
        {
            RegexEventArgs arg = e as RegexEventArgs;
            bool retVal = true;
            if (value != null)
            {
                Regex relationship = new Regex(arg.Expression);
                retVal = relationship.IsMatch((string)value);
            }
            return retVal;
        }

        public override ValidationRuleType Type
        {
            get { return ValidationRuleType.Error; }
        }
    }
}
